<template>
    <div class="UpdateUserInfo">
        <Title2>
            <div slot="back">
                <van-icon name="arrow-left" @click="$router.go(-1)"/>
            </div>
            <div slot="title">个人信息</div>
            <div slot="search" class="ddd" @click="save">
                保存
            </div>
        </Title2>
        <div class="content">
            <input type="text" v-model="value" v-if="this.type=='nickname'">
            <div v-if="this.type=='sex'" class="sex">
                <div @click="value=0">
                    <span>男</span>
                    <van-icon name="success" v-show="value==0"/>
                </div>
                <div @click="value=1">
                    <span>女</span>
                    <van-icon name="success" v-show="value==1"/>
                </div>
            </div>
            <div v-if="this.type=='attr'" class="attr">
                <div v-for="(item, index) in courseList" :key="index" @click="value=item.id">
                    <span>{{item.name}}</span>
                    <van-icon name="success" v-show="value==item.id"/>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
import Title2 from '../components/common/Title2';
import {updateUser} from '../untils/netWork';
    export default {
        data:()=>({
            value:"",
            type:"",
            courseList:[
                {name:"语文",id:7},
                {name:"数学",id:8},
                {name:"英语",id:9},
                {name:"物理",id:12},
                {name:"化学",id:13},
                {name:"生物",id:24},
                {name:"政治",id:25},
                {name:"信息技术",id:26}
            ]
        }),
        components:{
            Title2
        },
        methods:{
            save(){
                if(this.type=="sex"){
                    updateUser({sex:this.value}).then(res=>{
                        this.$router.push("/mineInfo")
                    })
                }
                if(this.type=="nickname"){
                    updateUser({nickname:this.value}).then(res=>{
                        this.$router.push("/mineInfo")
                    })
                }
                if(this.type=="attr"){
                    var data={
                        user_attr:JSON.stringify([{"attr_id":this.$route.query.payload.attr_id,"attr_val_id":this.$route.query.payload.attr_val_id},{"attr_id":2,"attr_val_id":this.value}])
                    }
                    // console.log(data)
                    updateUser(data).then(res=>{
                        this.$router.push("/mineInfo")
                        // console.log(res)
                    })
                }
                
            },
            // a(id){
            //     console.log(id)
            // }
        },
        activated(){
            this.type=this.$route.query.type
            this.value=this.$route.query.data
            // console.log(this.type,this.value)
            if(this.$route.query.payload){
                console.log(this.$route.query.payload)
            }
        }
    }
</script>

<style lang="scss" scoped>
.content{
    width: 90%;
    margin: 0 auto;
    input{
        border: 0;
        line-height: 50px;
    }
    .sex,
    .attr{
        line-height: 40px;
        div{
            display: flex;
            justify-content: space-between;
            align-items: center;
            .van-icon{
                color:sandybrown;
                font-size: 20px;
            }
        }
    }

}
</style>